/*
 * @Description: 聊天
 * @Author: iamsmiling
 * @Date: 2021-06-01 21:28:40
 * @LastEditTime: 2021-06-02 23:02:11
 */
import 'package:credit_business_circle/domain/remote/message/chat_record_entity.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

class ChatController extends GetxController {
  List<ChatRecordEntity> records = [
    ChatRecordEntity(content: "您好，我对贵公司的资源非常感兴趣，希望能有机会一起合作。", isMe: true),
    ChatRecordEntity(content: "你好，我是杭州嘉琪公司的业务经理， 请说一下贵公司比较擅长什么？", isMe: false),
    ChatRecordEntity(content: "好多", isMe: true),
    ChatRecordEntity(content: "我们公司有非常多的渠道资源，在抖音 头条等平台都有百万粉丝的账号。", isMe: true)
  ];

  late TextEditingController editingController;

  late ScrollController scrollController;

  @override
  void onInit() {
    editingController = TextEditingController();
    scrollController = ScrollController();
    super.onInit();
  }

  @override
  void onClose() {
    editingController.dispose();
    super.onClose();
  }

  void add() {
    records.add(
      ChatRecordEntity(content: "好多", isMe: true),
    );
    jumpToBottom();
    update();
  }

  Future sendMessage(String val) {
    return Future.value().then((value) {
      editingController.text = "";
      records.add(ChatRecordEntity(content: val, isMe: true));
    }).whenComplete(jumpToBottom);
  }

  void jumpToBottom() {
    scrollController.jumpTo(scrollController.position.maxScrollExtent);
    update();
  }
}
